<template>
  <div class="login-body">
    <div class="login-container">
      <div class="lhead">
        <!-- <img class="logo" src="https://s.weituibao.com/1582958061265/mlogo.png" /> -->
        <div class="name">
          <div class="title">社邻拼·商家后台</div>
          <!-- <div class="tips">管理系统</div> -->
        </div>
      </div>
      <el-form label-position="top" :rules="rules" :model="ruleForm" ref="loginForm" class="login-form">
        <el-form-item label="账号" prop="mobile">
          <el-input type="text" v-model.trim="ruleForm.mobile" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model.trim="ruleForm.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item>
          <div style="color: #333">登录表示您已同意<a>《服务条款》</a></div>
          <el-button style="width: 100%" type="primary" @click="submitForm">立即登录</el-button>
          <el-checkbox v-model="checked" @change="!checked">下次自动登录</el-checkbox>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import {onMounted, reactive, ref, toRefs } from 'vue'
import { setEncrypt } from '@/utils'
import {useRoute, useRouter } from 'vue-router'
import { store } from '@/store'
export default {
  name: 'Login',
  setup() {
    const loginForm = ref(null)
    const router = useRouter()//全局路由的实例，是router构造方法的实例
    const route = useRoute()//表示当前的路由信息
    const state = reactive({
      ruleForm: {
        mobile: '',
        password: ''
      },
      checked: true,
      rules: {
        mobile: [
          { required: 'true', message: '账户不能为空', trigger: 'blur' }
        ],
        password: [
          { required: 'true', message: '密码不能为空', trigger: 'blur' }
        ]
      }
    })
    onMounted(() => {
       store.dispatch('decrement')
      // getToken(res=>{
      //   console.log(res.data)
      //   if(res.data.flag){
      //     res.data.data.expires_in = Date.now() + res.data.data.expires_in * 1000;
      //     localSet('tokenobj', res.data.data)
      //     localSet('keyinfo',res.data.data.keyInfo)
      //   }
      // })
      //CommonJS 模块输出的是一个值的拷贝，ES6 模块输出的是值的引用
      //CommonJS 模块是运行时加载，ES6 模块是编译时输出接口
      //CommonJS 模块的require()是同步加载模块，ES6 模块的import命令是异步加载，有一个独立的模块依赖的解析阶段。
    }) 
    const submitForm = async () => {
      loginForm.value.validate((valid) => {
        console.log(valid)
        if (valid) {
          let password=setEncrypt(state.ruleForm.password)
             console.log(password)
          store.dispatch('actionLogin',{mobile: state.ruleForm.mobile,password})
           //query要使用path来引入，params要使用name来引入，接受参数类似，分别是this.$route.query.name和this.$route.params.name
            //query传递后形成的路径：/router?id=666&name=zhangsan
            //query接受传递的参数：<h2>姓名：{{$route.query.name}}</h2>

            //params传递后形成的路径：/router/123,/router/zhangsan
            //params接受传递的参数：<p>我是{{$route.params.userId}}用户相关信息</p>,
        } else {
          console.log('error submit!!')
          return false;
        }
      })
    }
    const resetForm = () => {
      loginForm.value.resetFields();
    }
    return {
      ...toRefs(state),
      loginForm,
      submitForm,
      resetForm
    }
  }
}
</script>

<style scoped>
  .login-body {
    display: flex;
    justify-content: flex-end;
    align-items: center;
    width: 100%;
    background-color: #fff;
    /* background-image: linear-gradient(25deg, #077f7c, #3aa693, #5ecfaa, #7ffac2); */
    /* background-image: url(@/assets/login_bg.jpg); */
    background:left top url(@/assets/login_bg.jpg) no-repeat;
    background-size: 100% 100%;
  }
  .login-container {
    width: 420px;
    height: 500px;
    background-color: #fff;
    border-radius: 10px;
    margin-right: 10%;
    box-shadow: 0px 21px 41px 0px rgba(0, 0, 0, 0.2);
  }
  .lhead {
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 40px 0 20px 0;
  }
  .lhead img {
    width: 100px;
    height: 100px;
    margin-right: 20px;
  }
  .lhead .title {
    font-size: 28px;
    color: #1BAEAE;
    font-weight: bold;
  }
  .lhead .tips {
    font-size: 12px;
    color: #999;
  }
  .login-form {
    width: 70%;
    margin: 0 auto;
  }
</style>
<style>
  .el-form--label-top .el-form-item__label {
    padding: 0;
  }
  .login-form .el-form-item {
    margin-bottom: 12px;
  }
</style>